
(function namespace(window){
                        
    window.synchronousCall = function(delay) {
        var tick = (new Date()).getTime();
        var d= "";
        if(delay !== undefined) {
            d="&d=" + delay;
        }
        var message = $("<p>sending synchronous xhr request</p>");
        $("#messageQueue").append(message);
        var data = $.ajax({
            type: "POST",
            url: "ajax/10s.json.php?t=synch&v=" + tick + d,
            async: false,
            error: function(jqXHR, textStatus, error) {
                alert("error " + textStatus);
            }
        }).responseJSON;
        message.append(" and response recieved");
    };
    
    window. asynchronousCall = function (delay) {
        var tick = (new Date()).getTime();
        var d = "";
        if(delay !== undefined) {
            d="&d=" + delay;
        }
        var message;            
        $.ajax({
            type: "POST",
            url: "ajax/10s.json.php?t=asynch&v=" + tick + d,
            //async: false,
            error: function(jqXHR, textStatus, error) {
                alert("error " + textStatus);
            }
        }).done(
            function(data) {
                message.append(" and response recieved");
            }
        );
        message = $("<p>asynchronous xhr request sent</p>");
        $("#messageQueue").append(message);
    };
    
    window.init = function(){

        $("#synchronous").click(function(event){
            event.preventDefault();
            synchronousCall();
        });    
        
        $("#asynchronous").click(function(event){
            event.preventDefault();
            asynchronousCall();
        });

    };
    
    function getHashParams() {
        var vars = {};
        var hashString = '';
        var hashes = [];
        if(window.location.hash) {
            hashString = window.location.hash.split('#')[1];
            hashes = hashString.split('&');
            
            for(var i=0; i<hashes.length; i++) {
                hash=hashes[i].split('=');
                        vars[hash[0]] = hash[1];
                    }
                }
                return vars;  
            }
            
            window.performInitialActions = function(){
                try {
                    var hashVars = [];
                    var synchDelay = 1;
                    var asynchDelay = 1;
                    hashVars = getHashParams();
                    synchDelay = hashVars.sd || synchDelay;
                    asynchDelay = hashVars.ad || asynchDelay;
                    if(hashVars.a !== undefined) {
                        if($.isNumeric(hashVars.a) && hashVars.a > 0){
                            for(var i =0; i < hashVars.a; i++) {
                                asynchronousCall(asynchDelay);
                            }
                        }
                    }                
                    
                    if(hashVars.s !== undefined) {
                        if($.isNumeric(hashVars.s) && hashVars.s > 0){
                            for(var i =0; i < hashVars.s; i++) {
                                synchronousCall(synchDelay);
                            }
                        }
                    }
                } catch(e){
                    alert(e);
                }

            };
            
})(window); 
        
  
$(function(){
    init();
    performInitialActions();
});
        